package com.dfqy.controller;

import com.dfqy.config.EventConfig;
import com.dfqy.pojo.*;
import com.dfqy.service.EventService;
import com.dfqy.vo.Page;
import com.dfqy.vo.Result;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Author : atyushu
 * Package : com.dfqy.controller
 * ClassName : EventController
 * Create : 2023/8/1 - 19:30
 * Version : v1.0
 * Description : The class of controller for event.
 */
@Validated
@RestController
@RequestMapping("event")
public class EventController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private EventService eventService;

    @Autowired
    private EventConfig eventConfig;

    @InitBinder
    public void initBinder(WebDataBinder webDataBinder) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        simpleDateFormat.setLenient(false);
        webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(simpleDateFormat, true));
    }

    @GetMapping("getAddr")
    public Result getAddr() {
        String[] addr = eventConfig.getAddr();
        return addr == null ? Result.error("无数据") : Result.success(addr);
    }

    @GetMapping("getAllTitle")
    public Result getAllTitle() {
        List<Event> events = eventService.getAll();
        return events == null ? Result.error("无数据") : Result.success(events);
    }

    @GetMapping("eventPage")
    public Page<Event> page(@RequestParam(name = "page") int page,
                            @RequestParam(name = "limit") int limit,
                            @RequestParam(name = "poster", required = false) String poster,
                            @RequestParam(name = "stype", required = false) String stype,
                            @RequestParam(name = "title", required = false) String title
    ) {
        return eventService.page(page, limit, poster, stype, title);
    }

    @PostMapping("insertEvent")
    public Result insert(@RequestBody Event event, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token != null && redisTemplate.opsForValue().get(token) != null) {
            Admin admin = (Admin) redisTemplate.opsForValue().get(token);
            event.setPoster(admin.getRealName());
        } else {
            event.setPoster("root");
        }
        int insert = eventService.insert(event);
        return insert > 0 ? Result.success("发布成功") : Result.error("发布失败");
    }

    @GetMapping("getStype")
    public Result getStype() {
        String[] stype = eventConfig.getStype();
        return stype == null ? Result.error("无数据") : Result.success(stype);
    }

    @GetMapping("deleteEvent/{id}")
    public Result delete(@PathVariable long id) {
        int delete = eventService.delete(id);
        return delete > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }

    @GetMapping("batchDeleteEvent/{id}")
    public Result batchDelete(@PathVariable String id) {
        String[] idStr = id.split(",");
        long[] ids = new long[idStr.length];
        for (int i = 0; i < idStr.length; i++) {
            ids[i] = Long.parseLong(idStr[i]);
        }
        int delete = eventService.delete(ids);
        return delete > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }

    @PostMapping("updateEvent")
    public Result update(@RequestBody Event event) {
        int update = eventService.update(event);
        return update > 0 ? Result.success("修改成功") : Result.error("修改失败");
    }

    @GetMapping("getEventById/{id}")
    public Result getById(@PathVariable long id, HttpServletRequest request) {
        Event event = eventService.getById(id);
        return event != null ? Result.success(event) : Result.error("无数据");
    }
}
